package com.cskaoyan.opensource;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

/**
 * 创建日期: 2022/11/18 15:54
 *
 * @author ciggar
 */
public class DruidUtils {

    static DataSource dataSource;

    static {
        try {
            // 读取配置文件
            Properties properties = new Properties();
            properties.load(new FileInputStream("druid.properties"));

            // 创建数据库连接池
            dataSource = DruidDataSourceFactory.createDataSource(properties);
        }catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    // 获取连接的方法
    public static Connection getConnection(){

        Connection connection = null;
        try {
            connection = dataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }

    public static DataSource getDataSource() {
        return dataSource;
    }
}
